package com.cyt.video.customerMediaPlayer;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.view.Surface;

import com.ksyun.media.player.IMediaPlayer;
import com.ksyun.media.player.KSYMediaPlayer;

import java.io.IOException;
import java.lang.ref.SoftReference;

import cn.jzvd.JZMediaInterface;
import cn.jzvd.JZMediaManager;
import cn.jzvd.JzvdMgr;

/**
 * @author QiQi
 * @ClassName: JZKSYPlayer
 * @date 2018/10/29
 * @description
 */
public class JZKSYPlayer extends JZMediaInterface implements IMediaPlayer.OnPreparedListener,
        IMediaPlayer.OnBufferingUpdateListener,
        IMediaPlayer.OnCompletionListener,
        IMediaPlayer.OnErrorListener,
        IMediaPlayer.OnInfoListener,
        IMediaPlayer.OnSeekCompleteListener,
        IMediaPlayer.OnVideoSizeChangedListener,
        IMediaPlayer.OnTimedTextListener {

    private SoftReference<Context> reference;

    private KSYMediaPlayer ksyMediaPlayer;

    public JZKSYPlayer(Context context) {
        reference = new SoftReference<>(context);
    }

    @Override
    public void start() {
        ksyMediaPlayer.start();
    }

    @Override
    public void prepare() {
        ksyMediaPlayer = new KSYMediaPlayer.Builder(reference.get().getApplicationContext()).build();

        ksyMediaPlayer.setOnPreparedListener(this);
        ksyMediaPlayer.setOnCompletionListener(this);
        ksyMediaPlayer.setOnBufferingUpdateListener(this);
        ksyMediaPlayer.setOnVideoSizeChangedListener(this);
        ksyMediaPlayer.setOnErrorListener(this);
        ksyMediaPlayer.setOnInfoListener(this);
        ksyMediaPlayer.setOnSeekCompleteListener(this);
        ksyMediaPlayer.setOnTimedTextListener(this);


        ksyMediaPlayer.setLooping(jzDataSource.looping);
        try {
            ksyMediaPlayer.setDataSource(jzDataSource.getCurrentUrl().toString());
            ksyMediaPlayer.setScreenOnWhilePlaying(true);
            ksyMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            ksyMediaPlayer.setVideoScalingMode(KSYMediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
            ksyMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void pause() {
        ksyMediaPlayer.pause();
    }

    @Override
    public boolean isPlaying() {
        return ksyMediaPlayer.isPlaying();
    }

    @Override
    public void seekTo(long time) {
        ksyMediaPlayer.seekTo(time);
    }

    @Override
    public void release() {
        if (ksyMediaPlayer != null) {
            ksyMediaPlayer.release();
        }
    }

    @Override
    public long getCurrentPosition() {
        return ksyMediaPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return ksyMediaPlayer.getDuration();
    }

    @Override
    public void setSurface(Surface surface) {
        ksyMediaPlayer.setSurface(surface);
    }

    @Override
    public void setVolume(float leftVolume, float rightVolume) {
        ksyMediaPlayer.setVolume(leftVolume, rightVolume);
    }

    @Override
    public void onBufferingUpdate(IMediaPlayer mp, int percent) {
        JZMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (JzvdMgr.getCurrentJzvd() != null) {
                    JzvdMgr.getCurrentJzvd().setBufferProgress(percent);
                }
            }
        });
    }

    @Override
    public void onCompletion(IMediaPlayer mp) {
        JZMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (JzvdMgr.getCurrentJzvd() != null) {
                    JzvdMgr.getCurrentJzvd().onAutoCompletion();
                }
            }
        });
    }

    @Override
    public boolean onError(IMediaPlayer mp, int what, int extra) {
        JZMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (JzvdMgr.getCurrentJzvd() != null) {
                    JzvdMgr.getCurrentJzvd().onError(what, extra);
                }
            }
        });
        return true;
    }

    @Override
    public boolean onInfo(IMediaPlayer mp, int what, int extra) {
        JZMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (JzvdMgr.getCurrentJzvd() != null) {
                    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                        JzvdMgr.getCurrentJzvd().onPrepared();
                    } else {
                        JzvdMgr.getCurrentJzvd().onInfo(what, extra);
                    }
                }
            }
        });
        return false;
    }

    @Override
    public void onPrepared(IMediaPlayer mp) {
        ksyMediaPlayer.setVideoScalingMode(KSYMediaPlayer.VIDEO_SCALING_MODE_NOSCALE_TO_FIT);
        ksyMediaPlayer.start();
        if (jzDataSource.getCurrentUrl().toString().toLowerCase().contains("mp3")) {
            JZMediaManager.instance().mainThreadHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (JzvdMgr.getCurrentJzvd() != null) {
                        JzvdMgr.getCurrentJzvd().onPrepared();
                    }
                }
            });
        }
    }

    @Override
    public void onSeekComplete(IMediaPlayer mp) {
        JZMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (JzvdMgr.getCurrentJzvd() != null) {
                    JzvdMgr.getCurrentJzvd().onSeekComplete();
                }
            }
        });
    }

    @Override
    public void onTimedText(IMediaPlayer mp, String text) {

    }

    @Override
    public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sar_num, int sar_den) {
        JZMediaManager.instance().currentVideoWidth = ksyMediaPlayer.getVideoWidth();
        JZMediaManager.instance().currentVideoHeight = ksyMediaPlayer.getVideoHeight();
        JZMediaManager.instance().mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                if (JzvdMgr.getCurrentJzvd() != null) {
                    JzvdMgr.getCurrentJzvd().onVideoSizeChanged();
                }
            }
        });
    }
}
